package day14.SemaphoreDemo01;

import java.util.concurrent.Semaphore;

/**
 * @author Xiao
 * @date 2021/8/8
 */
public class MySemaphore implements Runnable{
    //1.获得管理员对象管理通行证，最大允许2条线程同时访问
    private Semaphore sp = new Semaphore(2);
    @Override
    public void run() {
        //2.获取通行证
        try {
            sp.acquire();
            //开始行驶
            System.out.println("获取了通行证，准备开车了。。");
            Thread.sleep(100);
            //归还通行证
            System.out.println("归还通行证。。");
            sp.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
}
